/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityTransaction;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import model.Defectlog;
import model.Employee;
import model.Logstatus;
import model.Logtype;
import model.Priority;
import model.Severity;

/**
 *
 * @author HuyNVK
 */
@Stateless
public class DefectlogFacade extends AbstractFacade<Defectlog> implements DefectlogFacadeRemote {
    @PersistenceContext(unitName = "ejb-servPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DefectlogFacade() {
        super(Defectlog.class);
    }

    @Override
    public Defectlog merge(Defectlog defectlog) {
        return em.merge(defectlog);
    }

    @Override
    public void deleteByID(int id) {
        Defectlog defectlog = find(id);
        defectlog = em.merge(defectlog);
        em.remove(defectlog);
    }

    @Override
    public void insert(Defectlog defectlog) {
        em.persist(defectlog);
    }

    @Override
    public int countByLogType(Logtype logtype) throws Exception {
        Query query = em.createQuery("Select Count(p.logID) from Defectlog p where p.logTypeID.logTypeID='" 
                + logtype.getLogTypeID() + "'");
        Object obj = query.getSingleResult();
        return Integer.parseInt(obj.toString());
    }

    @Override
    public int countByStatus(Logstatus logstatus) throws Exception {
        Query query = em.createQuery("Select Count(p.logID) from Defectlog p where p.statusID.logStatusID='" 
                + logstatus.getLogStatusID() + "'");
        Object obj = query.getSingleResult();
        return Integer.parseInt(obj.toString());
    }

    @Override
    public int countByEmployee(Employee employee) throws Exception {
        Query query = em.createQuery("Select Count(p.logID) from Defectlog p where p.defectedBy.employeeID='" 
                + employee.getEmployeeID().toString() + "'");
        Object obj = query.getSingleResult();
        return Integer.parseInt(obj.toString());
    }

    @Override
    public int getMaxID() throws Exception {
        Query query = em.createQuery("Select Max(p.logID) from Defectlog p");
        Object obj = query.getSingleResult();
        return Integer.parseInt(obj.toString());
    }

    @Override
    public int countBySeverity(Severity severity) throws Exception {
        Query query = em.createQuery("Select Count(p.logID) from Defectlog p where p.severityID.severityID='" 
                + severity.getSeverityID().toString() + "'");
        Object obj = query.getSingleResult();
        return Integer.parseInt(obj.toString());
    }

    @Override
    public int countByPriority(Priority priority) throws Exception {
        Query query = em.createQuery("Select Count(p.logID) from Defectlog p where p.priorityID.priorityID='" 
                + priority.getPriorityID().toString() + "'");
        Object obj = query.getSingleResult();
        return Integer.parseInt(obj.toString());
    }

    @Override
    public List<Defectlog> search(String title, String statusID, String severityID, 
                                String priorityID, String logtypeID) throws Exception {
        String strQuery;
        strQuery = "Select q from Defectlog q where (:title is null or :title='' or q.title=:title)"
                    + " and (:status is null or :status='' or q.statusID.logStatusID=:status)"
                    + " and (:severity is null or :severity='' or q.severityID.severityID=:severity)"
                    + " and (:priority is null or :priority='' or q.priorityID.priorityID=:priority)"
                    + " and (:logtype is null or :logtype='' or q.logTypeID.logTypeID=:logtype)";
        Query query = em.createQuery(strQuery);
        query.setParameter("title", title);
        query.setParameter("status", statusID);
        query.setParameter("severity", severityID);
        query.setParameter("priority", priorityID);
        query.setParameter("logtype", logtypeID);
        
        return query.getResultList();
    }
}
